//
//  HomePageViewController.m
//  CapriceMusic
//
//  Created by 蒋恩浩 on 16/1/6.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "HomePageViewController.h"
#import "OfficialRecommendModel.h"
#import "OfficialWebViewController.h"
#import "HomePageTableViewCell.h"
#import "ChannelViewController.h"
@interface HomePageViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
/**
 *  model数组
 */
@property (nonatomic, strong) NSMutableArray *dataSource;
/**
 *  数据请求参数
 */
@property (nonatomic, assign) NSInteger num;

@end

@implementation HomePageViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *navTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    navTitle.text = @"首页";
    navTitle.textColor = [UIColor whiteColor];
    navTitle.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = navTitle;
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    [self setUpTableView];
    [self getData:0];
    [self getHeaderRefresh];
    [self getFootRefresh];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark - 网络请求
- (void)getData:(NSInteger)num {
    NSString *dataNum = [NSString stringWithFormat:@"%ld", (long)num];
    [ProgressHUD show:@"正在加载"];
    [JEHNetWorkTool getWithUrl:kOfficialRecommend parameter:@{@"rn":@"640*540", @"v":@"3", @"offset":dataNum, @"size":@"20"} httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} responseType:ResponseTypeJSON success:^(id result) {
        for (NSDictionary *dic in result) {
            OfficialRecommendModel *officialModel = [[OfficialRecommendModel alloc] initWithDictionary:dic];
            [self.dataSource addObject:officialModel];
        }
        [ProgressHUD dismiss];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        [ProgressHUD showError:@"请检查网络"];
        
        NSLog(@"%@", error);
    }];
}
//下拉刷新
- (void)getHeaderRefresh {
    __weak typeof(self) weakSelf = self;
    [self.tableView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.num = 0;
            [weakSelf.dataSource removeAllObjects];
            [weakSelf getData:weakSelf.num];
            [weakSelf.tableView headerEndRefreshing];
        });
    }];
}
//上拉加载
- (void)getFootRefresh {
    __weak typeof(self) weakSelf = self;
    [self.tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.num += 20;

            [weakSelf getData:weakSelf.num];
            [weakSelf.tableView footerEndRefreshing];
        });
    }];
}
#pragma mark - tableView
- (void)setUpTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 64) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor blackColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[HomePageTableViewCell class] forCellReuseIdentifier:@"HomePageTableViewCellIdentifier"];
    [self.view addSubview:self.tableView];
}

#pragma mark - tableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomePageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomePageTableViewCellIdentifier"];
    cell.backgroundColor = [UIColor blackColor];
    cell.officialModel = self.dataSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.height / 1.7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OfficialRecommendModel *officialRecommend = self.dataSource[indexPath.row];
    DetailViewController *detail = [[DetailViewController alloc] init];
    NSString *type = officialRecommend.typeq;
    if ([type isEqualToString:@"ACTIVITY"] || [type isEqualToString:@"AD"] || [type isEqualToString:@"INVENTORY"]) {
        OfficialWebViewController *offWebVC = [[OfficialWebViewController alloc] init];
        offWebVC.officialUrl = officialRecommend.urls;
        [self.navigationController pushViewController:offWebVC animated:YES];
    }else {
        detail.appendingId = officialRecommend.ids;
        detail.type = officialRecommend.typeq;
        [self.navigationController pushViewController:detail animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
